package com.cy.pj.goods.dao;

import com.cy.pj.goods.pojo.Goods;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * 此类类似于mybatis底层帮我们创建的动态代理实现类
 */
@Repository
public class GoodsDaoImpl implements GoodsDao {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public GoodsDaoImpl(){
        System.out.println("GoodsDaoImpl()");
    }

    @Override
    public List<Goods> findGoods() {
        //1.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.基于SqlSession实现商品查询
        String statement = "com.cy.pj.goods.dao.GoodsDao.findGoods"; //namespace+elementId
        List<Goods> list = sqlSession.selectList(statement);
        //3.释放资源
        sqlSession.close();
        //4.返回结果
        return list;
    }
}
